package cn.edu.wyu.blogmvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/post")
public class PostController {
    @Autowired
    PostDAOMock postDAOMock;

    @ModelAttribute("vList")
    public String[] getVList(){
        return new String[]{"所有人可见","朋友可见","仅自己可见"};
    }

    @GetMapping("/publish")
    public String publish(){
        return "publish";
    }

    @PostMapping("/publish")
    public String save(Post post, Model model){
        String content = post.getContent();
        if (post.getContent().length() > 140){
            model.addAttribute("errMsg","字符超过140个字符");
            return "publish";
        }else if (post.getContent().length() > 0 ){
//            保存
            System.out.printf("保存博文内容：%s",content);
            System.out.println();
            postDAOMock.add(post);
            return "redirect:/timeline/my";
        }
        return "publish";
    }

    @GetMapping("/remove/{id}}")
    public String remove(@PathVariable("id") String uuid){
        postDAOMock.remove(uuid);
        return "redirect:/timeline/my";
    }
}
